module Common where

type Offset = Int
data Operation = !Offset :+= !Int |  !Offset :<- !Int | PutcOp CharSrc | GetcOp | MoveOp !Offset
    | LoopOp LoopType [Operation] | OpLocAddT !Offset !Offset !Int | OpLocSetT !Offset !Offset !Int 
  deriving (Show, Eq)

data LoopType = If | While | DoWhile deriving (Show,Eq)
data CharSrc = Const !Int | Ptr !Offset | Str [Int] deriving (Show,Eq)

type Transformation = ([Operation] -> [Operation])

showOperation :: Operation -> String
showOperation (LoopOp t b) = "LoopOp " ++ show t ++ "[\n" ++ (lineJoin . map ind) b  ++ "]"
 where ind = lineJoin . map (\p -> ' ':' ':p) . lines . showOperation 
showOperation n = show n

lineJoin :: [String] -> String
lineJoin = reverse . drop 1 . reverse . unlines
